﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Environment.Documents;
using System.Reflection;
using Kokomo.Environment;
using System.Windows.Markup;

namespace Kokomo.PeInspector.Reflection
{
	[ViewType(typeof(AssemblyOverviewView))]
	class AssemblyOverviewViewModel : DetailViewModel
	{
		public AssemblyOverviewViewModel(NavigationContext navigationContext, AssemblyIndex assemblyIndex)
			: base(navigationContext, null, UiString.FromString(Messages.OverviewTitle))
		{
			if (assemblyIndex == null) throw new ArgumentNullException("assemblyIndex");

			this.StatisticItems = assemblyIndex.GetStatistics();

			XmlnsPrefixAttribute[] xmlnsPrefixAttributes = (XmlnsPrefixAttribute[])assemblyIndex.Assembly.GetCustomAttributes(typeof(XmlnsPrefixAttribute), true);
			ILookup<string, string> xmlnsPrefixTable = xmlnsPrefixAttributes.ToLookup(x => x.XmlNamespace, x => x.Prefix);

			List<XmlNamespaceItem> xmlNamespaceItems = new List<XmlNamespaceItem>();
			XmlnsDefinitionAttribute[] xmlnsAttributes = (XmlnsDefinitionAttribute[])assemblyIndex.Assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute), true);
			foreach (var xmlnsAttribute in xmlnsAttributes)
			{
				string prefix = xmlnsPrefixTable[xmlnsAttribute.XmlNamespace].FirstOrDefault();

				XmlNamespaceItem item = new XmlNamespaceItem()
				{
					XmlNamespace = xmlnsAttribute.XmlNamespace,
					CliNamespace = xmlnsAttribute.ClrNamespace,
					Alias = prefix
				};
				xmlNamespaceItems.Add(item);
			}
			this.XmlNamespaceItems = xmlNamespaceItems;
		}

		public IList<StatisticItem> StatisticItems { get; private set; }

		public bool HasXmlnamespaceItems { get { return (this.XmlNamespaceItems != null) && (this.XmlNamespaceItems.Count > 0); } }
		public IList<XmlNamespaceItem> XmlNamespaceItems { get; private set; }
	}
}
